using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace SurveyHost.Core.DAL
{
	/// <summary>
	/// Strongly-typed collection for the Survey class.
	/// </summary>
	[Serializable]
	public partial class SurveyCollection : ActiveList<Survey, SurveyCollection> 
	{	   
		public SurveyCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Survey table.
	/// </summary>
	[Serializable]
	public partial class Survey : ActiveRecord<Survey>
	{
		#region .ctors and Default Settings
		
		public Survey()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Survey(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Survey(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Survey(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Survey", TableType.Table, DataService.GetInstance("SurveyHost"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarSurveyId = new TableSchema.TableColumn(schema);
				colvarSurveyId.ColumnName = "SurveyId";
				colvarSurveyId.DataType = DbType.Guid;
				colvarSurveyId.MaxLength = 0;
				colvarSurveyId.AutoIncrement = false;
				colvarSurveyId.IsNullable = false;
				colvarSurveyId.IsPrimaryKey = true;
				colvarSurveyId.IsForeignKey = false;
				colvarSurveyId.IsReadOnly = false;
				colvarSurveyId.DefaultSetting = @"";
				colvarSurveyId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarSurveyId);
				
				TableSchema.TableColumn colvarBeginTime = new TableSchema.TableColumn(schema);
				colvarBeginTime.ColumnName = "BeginTime";
				colvarBeginTime.DataType = DbType.DateTime;
				colvarBeginTime.MaxLength = 0;
				colvarBeginTime.AutoIncrement = false;
				colvarBeginTime.IsNullable = false;
				colvarBeginTime.IsPrimaryKey = false;
				colvarBeginTime.IsForeignKey = false;
				colvarBeginTime.IsReadOnly = false;
				
						colvarBeginTime.DefaultSetting = @"(getdate())";
				colvarBeginTime.ForeignKeyTableName = "";
				schema.Columns.Add(colvarBeginTime);
				
				TableSchema.TableColumn colvarEndTime = new TableSchema.TableColumn(schema);
				colvarEndTime.ColumnName = "EndTime";
				colvarEndTime.DataType = DbType.DateTime;
				colvarEndTime.MaxLength = 0;
				colvarEndTime.AutoIncrement = false;
				colvarEndTime.IsNullable = true;
				colvarEndTime.IsPrimaryKey = false;
				colvarEndTime.IsForeignKey = false;
				colvarEndTime.IsReadOnly = false;
				colvarEndTime.DefaultSetting = @"";
				colvarEndTime.ForeignKeyTableName = "";
				schema.Columns.Add(colvarEndTime);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["SurveyHost"].AddSchema("Survey",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("SurveyId")]
		public Guid SurveyId 
		{
			get { return GetColumnValue<Guid>("SurveyId"); }

			set { SetColumnValue("SurveyId", value); }

		}

		  
		[XmlAttribute("BeginTime")]
		public DateTime BeginTime 
		{
			get { return GetColumnValue<DateTime>("BeginTime"); }

			set { SetColumnValue("BeginTime", value); }

		}

		  
		[XmlAttribute("EndTime")]
		public DateTime? EndTime 
		{
			get { return GetColumnValue<DateTime?>("EndTime"); }

			set { SetColumnValue("EndTime", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
				
		private SurveyHost.Core.DAL.AnswerCollection colAnswerRecords;
		public SurveyHost.Core.DAL.AnswerCollection AnswerRecords
		{
			get
			{
				if(colAnswerRecords == null)
					colAnswerRecords = new SurveyHost.Core.DAL.AnswerCollection().Where(Answer.Columns.SurveyId, SurveyId).Load();
				return colAnswerRecords;			
			}

			set { colAnswerRecords = value; }

		}

		
		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		#region Many To Many Helpers
		
		 
		public SurveyHost.Core.DAL.StemCollection GetStemCollection() { return Survey.GetStemCollection(this.SurveyId); }

		public static SurveyHost.Core.DAL.StemCollection GetStemCollection(Guid varSurveyId)
		{
			SubSonic.QueryCommand cmd = new SubSonic.QueryCommand(
				"SELECT * FROM Stem INNER JOIN Answer ON "+
				"Stem.StemId=Answer.StemId WHERE Answer.SurveyId=@SurveyId", Survey.Schema.Provider.Name);
			
			cmd.AddParameter("@SurveyId", varSurveyId, DbType.Guid);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			StemCollection coll = new StemCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}

		
		public static void SaveStemMap(Guid varSurveyId, StemCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM Answer WHERE SurveyId=@SurveyId", Survey.Schema.Provider.Name);
			cmdDel.AddParameter("@SurveyId", varSurveyId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Stem item in items)
			{
				Answer varAnswer = new Answer();
				varAnswer.SetColumnValue("SurveyId", varSurveyId);
				varAnswer.SetColumnValue("StemId", item.GetPrimaryKeyValue());
				varAnswer.Save();
			}

		}

		public static void SaveStemMap(Guid varSurveyId, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM Answer WHERE SurveyId=@SurveyId", Survey.Schema.Provider.Name);
			cmdDel.AddParameter("@SurveyId", varSurveyId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					Answer varAnswer = new Answer();
					varAnswer.SetColumnValue("SurveyId", varSurveyId);
					varAnswer.SetColumnValue("StemId", l.Value);
					varAnswer.Save();
				}

			}

		}

		public static void SaveStemMap(Guid varSurveyId , Guid[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM Answer WHERE SurveyId=@SurveyId", Survey.Schema.Provider.Name);
			cmdDel.AddParameter("@SurveyId", varSurveyId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Guid item in itemList) 
			{
				Answer varAnswer = new Answer();
				varAnswer.SetColumnValue("SurveyId", varSurveyId);
				varAnswer.SetColumnValue("StemId", item);
				varAnswer.Save();
			}

		}

		
		public static void DeleteStemMap(Guid varSurveyId) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM Answer WHERE SurveyId=@SurveyId", Survey.Schema.Provider.Name);
			cmdDel.AddParameter("@SurveyId", varSurveyId);
			DataService.ExecuteQuery(cmdDel);
		}

		
		#endregion
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varSurveyId,DateTime varBeginTime,DateTime? varEndTime)
		{
			Survey item = new Survey();
			
			item.SurveyId = varSurveyId;
			
			item.BeginTime = varBeginTime;
			
			item.EndTime = varEndTime;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varSurveyId,DateTime varBeginTime,DateTime? varEndTime)
		{
			Survey item = new Survey();
			
				item.SurveyId = varSurveyId;
				
				item.BeginTime = varBeginTime;
				
				item.EndTime = varEndTime;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string SurveyId = @"SurveyId";
			 public static string BeginTime = @"BeginTime";
			 public static string EndTime = @"EndTime";
						
		}

		#endregion
	}

}

